/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller.web;

import business.User;
import business.DaftarUser;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author atikahSH
 */
public class ProfileEditServlet extends Servlet {


    public ProfileEditServlet(){
        super();
        setTemplate("/WEB-INF/edit_profil.jsp");
    }

    @Override
    protected void process() {

        setMessage("");
        HttpSession session = getRequest().getSession();
                if (validate_field()) {
                        DaftarUser du = new DaftarUser();
                        String uname = (String) session.getAttribute("username");
                        User u = du.getUser(getRequest().getParameter("username"));
                            u.setUsername(getRequest().getParameter("username"));
                            u.setPassword(getRequest().getParameter("password"));
                            u.setEmail(getRequest().getParameter("email"));
                            u.setNama_depan(getRequest().getParameter("nama_depan"));
                            u.setNama_belakang(getRequest().getParameter("nama_belakang"));
                            u.settelepon(getRequest().getParameter("telepon"));
                            u.setalamat(getRequest().getParameter("alamat"));
                            u.setkota(getRequest().getParameter("kota"));
                            u.setprovinsi(getRequest().getParameter("provinsi"));
                            u.setnegara(getRequest().getParameter("negara"));
                            u.setKodePos(getRequest().getParameter("KodePos"));
                            du.editUser(u);
                } else {
                try {
                    getResponse().sendRedirect("dataKosong");
                } catch (IOException ex) {
                    Logger.getLogger(ProfileEditServlet.class.getName()).log(Level.SEVERE, null, ex);
                     }
                    }
    }


    boolean check() {
     HttpSession session = getRequest().getSession();
        if (session.getAttribute("username") == null) {
            //members uname = (members) session.getAttribute("username");
            return false;
        }
        return true;
    }

    boolean validate_field() {

        String username = getRequest().getParameter("username");
        String password = getRequest().getParameter("password");
        String nama_depan = getRequest().getParameter("nama_depan");
        String nama_belakang = getRequest().getParameter("nama_belakang");
        String alamat = getRequest().getParameter("alamat");
        String telepon = getRequest().getParameter("telepon");
        String kota = getRequest().getParameter("kota");
        String provinsi = getRequest().getParameter("provinsi");
        String negara = getRequest().getParameter("negara");
        String KodePos = getRequest().getParameter("KodePos");
        String email = getRequest().getParameter("email");
        if(username == null||password == null||alamat == null||telepon == null||email == null){
            return false;
        }

        return true;
    }

}
